package com.cml.client.domain.service;

import com.cml.client.common.context.ApplicationContext;
import com.cml.client.domain.entity.Server;
import com.cml.client.infrastructure.MessageQueueManager;
import com.cml.client.infrastructure.NetworkManager;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.nio.channels.SocketChannel;

/**
 * 姓名：陈茂林
 * 时间：2023/12/6
 * 描述：
 */
public class ServerHAService {
    private Logger logger = Logger.getLogger(ServerHAService.class);
    private  ApplicationContext applicationContext;
    private  ServerHAService(){}
    private static class Singleton{
        static  ServerHAService serverHAService = new ServerHAService();
    }

    public  static ServerHAService getInstance(ApplicationContext applicationContext){
        applicationContext = applicationContext;
        return Singleton.serverHAService;
    }

    public void ha(Server servers){
        clearResource(servers);
        try{
            applicationContext.getServiceInstanceService().initServerInstance();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }

    private void clearResource(Server server){
        applicationContext.getNetworkManager().clearResource(server);
    }
}
